
import Eureka


//添加工作经验表单

class addWorkExperienceController : FormViewController {

    private let waiting=waitingAlertUtil(msg: "请稍后..")

    //传过来的resumeId
    var resumeId:String = String()
    
	override func viewDidLoad() {
        super.viewDidLoad()

        // 日期转换器
        let DateFormatter=NSDateFormatter()
        DateFormatter.dateFormat="yyyy-MM-dd"

        URLRow.defaultCellUpdate = { cell, row in cell.textField.textColor = .blueColor() }
        LabelRow.defaultCellUpdate = { cell, row in cell.detailTextLabel?.textColor = .orangeColor()  }
        CheckRow.defaultCellSetup = { cell, row in cell.tintColor = .orangeColor() }
        // 设置最大最小日期
        DateRow.defaultRowInitializer = { row in 
            row.maximumDate = DateFormatter.dateFromString("2015-12-31")
            row.minimumDate = DateFormatter.dateFromString("1950-01-01")
        }

        form +++ Section()
            <<< TextRow("companyName"){ row in
                row.title = "公司名称"
                row.placeholder = "输入公司名称"
            }
            <<< PushRow<String>("companyNature"){
                $0.title = "公司性质"
                $0.selectorTitle = "公司性质"
                var array=enumUtil.getOrderedStringArray(enumUtil.COMPANY_NATURE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("companySize"){
                $0.title = "公司规模"
                $0.selectorTitle = "公司规模"
                var array=enumUtil.getOrderedStringArray(enumUtil.COMPANY_SCALE)
                $0.options = array
                $0.value = array[0]
            }
            <<< PushRow<String>("companyIndustry"){
                $0.title = "所属行业"
                $0.selectorTitle = "所属行业"
                var array=enumUtil.getOrderedStringArray(enumUtil.INDUSRTY_ENUM)
                $0.options = array
                $0.value = array[0]
            }
            <<< TextRow("subFunction"){ row in
                row.title = "担任职位"
                row.placeholder = "输入职位"
            }
            <<< TextAreaRow("responsiblity"){
                $0.title = "工作描述"
                $0.placeholder = "输入工作描述"
            }

        +++ Section() 
            <<< DateRow("startdate"){
                    $0.title = "开始时间"
                    $0.value = DateFormatter.dateFromString("2010-01-01")
                }
            <<< DateRow("enddate"){
                    $0.title = "结束时间"
                    $0.value = DateFormatter.dateFromString("2010-01-01")
                }

        +++ Section()
            <<< ButtonRow() { (row: ButtonRow) -> Void in
                   row.title = "添加工作经历"
                row.cell.tintColor = UIColor.hexStringToColor("#ffffff")
                row.cell.backgroundColor = UIColor.hexStringToColor("#ff4081")
                }  .onCellSelection({ (cell, row) in
                    self.submitForm()
                })


    }


    private func submitForm(){
        // 等待提示
//        waiting.show(self)
        //获取表单结果
        let dict = form.values()

        //由segue传过来的resumeId
        let resumeId = self.resumeId
        let companyName = dict["companyName"] as! String
        let companyNature = enumUtil.getKeyByString(dict["companyNature"] as! String,dict: enumUtil.COMPANY_NATURE)
        let companySize = enumUtil.getKeyByString(dict["companySize"] as! String,dict: enumUtil.COMPANY_SCALE)
        let companyIndustry = String( enumUtil.getKeyByString(dict["companyIndustry"] as! String,dict: enumUtil.INDUSRTY_ENUM) )

        let startdate = dict["startdate"] as! NSDate
        let enddate = dict["enddate"] as! NSDate

        let subFunction = dict["subFunction"] as! String
        let responsiblity = dict["responsiblity"] as! String

        resumeManager.AddWorkExperience(resumeId,
                             startdate:startdate,
                             enddate:enddate,
                             companyName:companyName,
                             companyNature:companyNature,
                             companySize:companySize,
                             companyIndustry:companyIndustry,
                             subFunction:subFunction,
                             responsiblity:responsiblity,
                             successCallback: { (msg) in
                    //            添加信息ok
//                                self.waiting.stop()
                    //            退出
                                self.navigationController?.popViewControllerAnimated(true)
                                }) { (error) in
                                    self.waiting.stop()
                                    warningAlertUtil(title: "错误", msg: error).show(self)
                            }
    }

}


